<template>
  <div class="pd-20">
    <el-input v-model="name"
              placeholder="请输入合并症名称"
              class="w-200"
              clearable
              @clear="handleQuery"></el-input>
    <el-button type="primary"
               @click="handleQuery">查 询</el-button>
    <el-button type="primary"
               @click="handleOption(null, 'add')">新 增</el-button>
    <element-table :columns="columns"
                   class="mt-20"
                   :data="tableData"
                   v-loading="loading"
                   pager
                   :pageTotal="pageTotal"
                   :pageIndex="pageIndex"
                   :pageSize="pageSize"
                   @handleChangePage="handleChangePage">
      <template v-slot:edit="{row}">
        <el-button type="primary"
                   @click="handleOption(row, 'edit')">编 辑</el-button>
        <el-button type="danger"
                   @click="handleOption(row, 'del')">删 除</el-button>
      </template>
    </element-table>
    <complications-dialog :dataObj="dataObj"
                          v-if="isShow"
                          :visible.sync="isShow"></complications-dialog>
  </div>
</template>
<script>
  import elementTable from '@/base/ElementTable'
  import complicationsDialog from './complicationsDialog'
  export default {
    name: 'complications', // 合并症管理
    data () {
      return {
        loading: false,
        isShow: false,
        columns: [
          {
            label: '合并症名称',
            prop: 'name',
          },
          {
            label: '关联监测项',
            prop: 'device',
          },
          {
            label: '创建时间',
            prop: 'createTime',
            width: 150,
            formatter: (row, cellValue) => {
              return this.commonFunction.timestampFormat(new Date(row.createTime), 'yyyy-MM-dd hh:mm:ss')
            }
          },
          {
            label: '操作',
            prop: 'edit',
            isSlot: true
          }
        ],
        tableData: [],
        pageTotal: 0,
        pageIndex: 1,
        pageSize: 20,
        dataObj: {},
        name: ''
      }
    },
    methods: {
      handleQuery() {
        this.pageIndex=1
        this.getList()
      },
      getList () {
        this.post(this.api.promote.complicationList, {
          pageIndex: this.pageIndex - 1,
          pageSize: this.pageSize,
          name: this.name
        }).then(res => {
          if (res.data.code === '200') {
            this.tableData = res.data.object;
            this.pageTotal = res.data.totalNum;
          }
        })
      },
      handleOption (row, op) {
        switch (op) {
          case 'add':
            this.dataObj = null;
            this.isShow = true;
            break;
          case 'edit':
            this.isShow = true;
            this.dataObj = Object.assign({}, row);
            break;
          case 'del':
            this.$confirm('您是否要删除该合并症？')
              .then(async _ => {
                const { data: res } = await this.post(this.api.promote.delComplication, {
                  id: row.id
                })
                if (res.code === '200') {
                  this.$message.success('删除成功!')
                  this.getList()
                }
              })
              .catch(_ => { });
            break;
        }
      },
      // 分页
      handleChangePage (page) {
        this.pageIndex = page;
        this.getList();
      },
    },
    created () {
      this.getList();
    },
    components: {
      'element-table': elementTable,
      'complications-dialog': complicationsDialog
    }
  }
</script>
<style scoped lang="scss">
</style>
